﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2011
*/
#region
using System;
using System.Collections.Generic;
using System.Data;
using NetBay.Core.BusinessEntities.Versioning;

#endregion

namespace Rio.Manager.Business.Repository.Translator
{
    public class VersioningTranslator
    {
        /// <summary>
        /// GetVersionModel translation
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static VersioningModel GetVersionModel(IDataReader P_oDbDataReader)
        {
            VersioningModel _models = new VersioningModel();
            try
            {
                using (P_oDbDataReader)
                {
                    if (P_oDbDataReader.Read())
                    {
                        _models.Identifier = int.Parse(P_oDbDataReader["RDVM_ID"].ToString());
                        _models.ModelType = (VersionType)Enum.Parse(typeof(VersionType), P_oDbDataReader["T_REF_MODEL_ID"].ToString());
                        _models.MaxMajorVersion = int.Parse(P_oDbDataReader["RDVM_MAXMAJOR"].ToString());
                        _models.MaxMinorVersion = int.Parse(P_oDbDataReader["RDVM_MAXMINOR"].ToString());
                        _models.WorkFlow = int.Parse(P_oDbDataReader["RDVM_APPROB_WKF_ID"].ToString());
                        _models.IsEnabled = Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RDVM_ACTIF"].ToString()));
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("EXCEPTION -> " + Environment.NewLine + ex);
            }
            return _models;
        }

        /// <summary>
        /// GetCheckOutVersionByFolderType translation
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static List<VersioningItem> GetCheckOutVersionByFolderType(IDataReader P_oDbDataReader)
        {
            var l_lstVersionCheckOut = new List<VersioningItem>(); 
            
            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        VersioningItem _versionCheckOut = new VersioningItem();
                        _versionCheckOut.VersionId = Convert.ToInt32(P_oDbDataReader["RDV_ID"].ToString());
                        _versionCheckOut.DocumentPath = P_oDbDataReader["RDV_FILENAME"].ToString();
                        _versionCheckOut.CreatedBy = P_oDbDataReader["USERCREE"].ToString();
                        _versionCheckOut.CreatedOn = DateTime.Parse(P_oDbDataReader["RDV_DATECREE"].ToString());
                        _versionCheckOut.State = VersionState.Checkout;  
                        l_lstVersionCheckOut.Add(_versionCheckOut);                      
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_lstVersionCheckOut;
        }

        /// <summary>
        /// HasCheckoutVersionByFolderType translation
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public static bool HasCheckoutVersionByFolderType(IDataReader P_oDbDataReader)
        {
            bool result = false;
            try
            {
                using (P_oDbDataReader)
                {
                    result = Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RESULT"].ToString()));
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("EXCEPTION -> " + Environment.NewLine + ex);
            }
            return result;
        }
    }
}